#pragma once
#ifndef _CALIBRATIONCONTROL_H
#define _CALIBRATIONCONTROL_H

#include <Windows.h>
#include <GdiPlus.h>
#include <math.h>
#include <fstream>

#pragma comment (lib, "gdiplus.lib")

#include "../common/glenv.h"
#include "../common/myglStringControl.h"
#include "../common/ShowListManager.h"
#include "../common/SceneCoordSpaceInfo.h"
#include "../common/configure.h"
#include "../common/LightInputDriver.h"

// Configuration
#define UNDISTORT_NUM 5
#define DATA_NEED_PER_POINT 25


// Step defines
#define INITIAL 0
#define ADJUST_POSITION 1
#define CALIBRATION 2
#define CHECK 3
#define SAVE_DATA 4
#define COMPLETE 5

using namespace std;


class CalibrationControl : public GlControl
{
private:
  Ptr< SceneCoordSpaceInfo > _pSceneCoordSpaceInfo;
  Ptr< myglStringControl > _pStringControl;
  Ptr< ShowListManager > _pShowListMng;
  Ptr< LightInputDriver > _pLightInput;

  int _step;
  int _pointCollected;
  int _calibrationPointCurrentIndex;
  Array< float > _checkPoint;
  MyMatrix< float > _cameraPoint;
  MyMatrix< float > _projectPoint;
  int _calibrationPointIndexArr[UNDISTORT_NUM];

  bool _get_terrain_size (int& width, int& height);
  void _go_next_step () { ++_step; if (_step > COMPLETE) exit (0); }
  bool _add_camera_point_data (IN const Array< float >& newPoint);

public:
  CalibrationControl ();
  // enable opengl feature and set default background color here
  virtual void init_render ();
  virtual void draw ();
  virtual void handle_keypress (unsigned char key, int x, int y);
  virtual void handle_special_keypress (int key, int x, int y);
  virtual void handle_mouse (int button, int state, int x, int y);
  virtual int handle_timer (int val);
};

#endif